﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using ihongma.DBUtility;
using ihongma.Common;//Please add references
namespace ihongma.DAL
{
	/// <summary>
	/// 数据访问类:ajax
	/// </summary>
	public partial class ajax
	{
		public ajax()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperOleDb.GetMaxID("id", "dt_ajax"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from dt_ajax");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
			};
			parameters[0].Value = id;

			return DbHelperOleDb.Exists(strSql.ToString(),parameters);
		}


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(ihongma.Model.ajax model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dt_ajax(");
            strSql.Append("name,channel_id,category_id,is_top,img,Homepage,Download,Sample,contet,tag,addtime)");
            strSql.Append(" values (");
            strSql.Append("@name,@channel_id,@category_id,@is_top,@img,@Homepage,@Download,@Sample,@contet,@tag,@addtime)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@name", OleDbType.VarChar,50),
					new OleDbParameter("@channel_id", OleDbType.Integer,4),
					new OleDbParameter("@category_id", OleDbType.Integer,4),
					new OleDbParameter("@is_top", OleDbType.Integer,4),
					new OleDbParameter("@img", OleDbType.VarChar,100),
					new OleDbParameter("@Homepage", OleDbType.VarChar,100),
					new OleDbParameter("@Download", OleDbType.VarChar,100),
					new OleDbParameter("@Sample", OleDbType.VarChar,100),
					new OleDbParameter("@contet", OleDbType.VarChar,0),
					new OleDbParameter("@tag", OleDbType.VarChar,50),
					new OleDbParameter("@addtime", OleDbType.Date)};
            parameters[0].Value = model.name;
            parameters[1].Value = model.channel_id;
            parameters[2].Value = model.category_id;
            parameters[3].Value = model.is_top;
            parameters[4].Value = model.img;
            parameters[5].Value = model.Homepage;
            parameters[6].Value = model.Download;
            parameters[7].Value = model.Sample;
            parameters[8].Value = model.contet;
            parameters[9].Value = model.tag;
            parameters[10].Value = model.addtime;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ihongma.Model.ajax model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dt_ajax set ");
            strSql.Append("name=@name,");
            strSql.Append("channel_id=@channel_id,");
            strSql.Append("category_id=@category_id,");
            strSql.Append("is_top=@is_top,");
            strSql.Append("img=@img,");
            strSql.Append("Homepage=@Homepage,");
            strSql.Append("Download=@Download,");
            strSql.Append("Sample=@Sample,");
            strSql.Append("contet=@contet,");
            strSql.Append("tag=@tag,");
            strSql.Append("addtime=@addtime");
            strSql.Append(" where id=@id");
            OleDbParameter[] parameters = {
					new OleDbParameter("@name", OleDbType.VarChar,50),
					new OleDbParameter("@channel_id", OleDbType.Integer,4),
					new OleDbParameter("@category_id", OleDbType.Integer,4),
					new OleDbParameter("@is_top", OleDbType.Integer,4),
					new OleDbParameter("@img", OleDbType.VarChar,100),
					new OleDbParameter("@Homepage", OleDbType.VarChar,100),
					new OleDbParameter("@Download", OleDbType.VarChar,100),
					new OleDbParameter("@Sample", OleDbType.VarChar,100),
					new OleDbParameter("@contet", OleDbType.VarChar,0),
					new OleDbParameter("@tag", OleDbType.VarChar,50),
					new OleDbParameter("@addtime", OleDbType.Date),
					new OleDbParameter("@id", OleDbType.Integer,4)};
            parameters[0].Value = model.name;
            parameters[1].Value = model.channel_id;
            parameters[2].Value = model.category_id;
            parameters[3].Value = model.is_top;
            parameters[4].Value = model.img;
            parameters[5].Value = model.Homepage;
            parameters[6].Value = model.Download;
            parameters[7].Value = model.Sample;
            parameters[8].Value = model.contet;
            parameters[9].Value = model.tag;
            parameters[10].Value = model.addtime;
            parameters[11].Value = model.id;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from dt_ajax ");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from dt_ajax ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperOleDb.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ihongma.Model.ajax GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,name,channel_id,category_id,is_top,img,Homepage,Download,Sample,contet,tag,addtime from dt_ajax ");
			strSql.Append(" where id=@id");
			OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)
			};
			parameters[0].Value = id;

			ihongma.Model.ajax model=new ihongma.Model.ajax();
			DataSet ds=DbHelperOleDb.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["name"]!=null && ds.Tables[0].Rows[0]["name"].ToString()!="")
				{
					model.name=ds.Tables[0].Rows[0]["name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["channel_id"]!=null && ds.Tables[0].Rows[0]["channel_id"].ToString()!="")
				{
					model.channel_id=int.Parse(ds.Tables[0].Rows[0]["channel_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["category_id"]!=null && ds.Tables[0].Rows[0]["category_id"].ToString()!="")
				{
					model.category_id=int.Parse(ds.Tables[0].Rows[0]["category_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["is_top"]!=null && ds.Tables[0].Rows[0]["is_top"].ToString()!="")
				{
					model.is_top=int.Parse(ds.Tables[0].Rows[0]["is_top"].ToString());
				}
				if(ds.Tables[0].Rows[0]["img"]!=null && ds.Tables[0].Rows[0]["img"].ToString()!="")
				{
					model.img=ds.Tables[0].Rows[0]["img"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Homepage"]!=null && ds.Tables[0].Rows[0]["Homepage"].ToString()!="")
				{
					model.Homepage=ds.Tables[0].Rows[0]["Homepage"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Download"]!=null && ds.Tables[0].Rows[0]["Download"].ToString()!="")
				{
					model.Download=ds.Tables[0].Rows[0]["Download"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Sample"]!=null && ds.Tables[0].Rows[0]["Sample"].ToString()!="")
				{
					model.Sample=ds.Tables[0].Rows[0]["Sample"].ToString();
				}
				if(ds.Tables[0].Rows[0]["contet"]!=null && ds.Tables[0].Rows[0]["contet"].ToString()!="")
				{
					model.contet=ds.Tables[0].Rows[0]["contet"].ToString();
				}
				if(ds.Tables[0].Rows[0]["tag"]!=null && ds.Tables[0].Rows[0]["tag"].ToString()!="")
				{
					model.tag=ds.Tables[0].Rows[0]["tag"].ToString();
				}
				if(ds.Tables[0].Rows[0]["addtime"]!=null && ds.Tables[0].Rows[0]["addtime"].ToString()!="")
				{
					model.addtime=DateTime.Parse(ds.Tables[0].Rows[0]["addtime"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,name,channel_id,category_id,is_top,img,Homepage,Download,Sample,contet,tag,addtime ");
			strSql.Append(" FROM dt_ajax ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperOleDb.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM dt_ajax ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperOleDb.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from dt_ajax T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperOleDb.Query(strSql.ToString());
		}
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,name,channel_id,category_id,is_top,img,Homepage,Download,Sample,contet,tag,addtime ");
            strSql.Append(" FROM dt_ajax ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperOleDb.Query(strSql.ToString());
        }
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM dt_ajax");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperOleDb.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperOleDb.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			OleDbParameter[] parameters = {
					new OleDbParameter("@tblName", OleDbType.VarChar, 255),
					new OleDbParameter("@fldName", OleDbType.VarChar, 255),
					new OleDbParameter("@PageSize", OleDbType.Integer),
					new OleDbParameter("@PageIndex", OleDbType.Integer),
					new OleDbParameter("@IsReCount", OleDbType.Boolean),
					new OleDbParameter("@OrderType", OleDbType.Boolean),
					new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
					};
			parameters[0].Value = "dt_ajax";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

